package com.mera.tektronix.csviewer.jmx

import javax.management.*

import com.mera.tektronix.csviewer.domain.*

class ConfigurationMBean implements IConfigurationMBean {
	def possibleOrders = ["asc", "desc"]
	def classOrders = [
		                ("${Department.class.name}".toString()) : ['sort' : 'id', 'order' : 'asc']
		              ]
	@Override
	public void setDepartmentSorting(String field, String order) {		
		def res = Department.declaredFields.find { (it.name == field && it.getAnnotation(Sorted.class) != null) && possibleOrders.find { it == order } }
		
		if(res) {
			classOrders["${Department.class.name}"].sort = field
			classOrders["${Department.class.name}"].order = order
		} else {
			throw new ReflectionException(
				new NoSuchFieldException(field),
				"Field: " + field + " or Order: " + order + " are irrelevant for " + Department.class.name);
		}		
	}
}
